package com.dmai.capacity.platform.user.service.impl;

import com.dmai.capacity.platform.common.constant.CodeEnum;
import com.dmai.capacity.platform.common.constant.CommonConstant;
import com.dmai.capacity.platform.common.dto.PageResult;
import com.dmai.capacity.platform.common.dto.PageResultData;
import com.dmai.capacity.platform.user.mapper.SysMenuButtonDao;
import com.dmai.capacity.platform.user.model.SysMenuButton;
import com.dmai.capacity.platform.user.service.ISysMenuButtonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;


@Service
public class SysMenuButtonServiceImpl implements ISysMenuButtonService {

    @Autowired
    private SysMenuButtonDao sysMenuButtonDao;

    /**
     * 添加
     *
     * @param sysMenuButton
     */
    public int save(SysMenuButton sysMenuButton) {
        return sysMenuButtonDao.save(sysMenuButton);
    }

    /**
     * 修改
     *
     * @param sysMenuButton
     */
    public int update(SysMenuButton sysMenuButton) {
        return sysMenuButtonDao.update(sysMenuButton);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return sysMenuButtonDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SysMenuButton findById(String id) {
        return sysMenuButtonDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SysMenuButton> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT) != null && MapUtils.getInteger(params, CommonConstant.PAGE_SIZE) != null)
            PageHelper.startPage(MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT), MapUtils.getInteger(params, CommonConstant.PAGE_SIZE), true);

        List<SysMenuButton> list = sysMenuButtonDao.findAll(params);
        PageInfo<SysMenuButton> pageInfo = new PageInfo(list);

        return PageResult.<SysMenuButton>builder().data(PageResultData.<SysMenuButton>builder().records(pageInfo.getList()).total(pageInfo.getTotal()).pages(pageInfo.getPages()).build()).code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();

    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SysMenuButton> findAll(Map<String, Object> params) {
        return sysMenuButtonDao.findAll(params);
    }

}
